package com.cloud.base.thread.countDownLatch;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchMain {
    public static void main(String[] args) throws InterruptedException {
        // 创建一个计数器
        // 5个线程结束之后，再去执行主线程
        CountDownLatch latch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + ":开始");
                    Thread.sleep(2000);
                    System.out.println(Thread.currentThread().getName() + ":结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                latch.countDown();
            }).start();
        }
        latch.await();
        System.out.println("主线程执行");
    }
}
